package com.keai.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.keai.dao.PermissionDao;
import com.keai.entity.PageResult;
import com.keai.entity.QueryPageBean;
import com.keai.pojo.Permission;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: SpringMVC_test
 * @author: Mr.Dong
 * @create: 2019-08-24 16:37
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    PermissionDao permissionDao;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //2.条件查询
        Page<Permission> page=permissionDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page);

    }

    @Override
    @Transactional
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    @Override
    @Transactional
    public void delById(Integer id) {
        //1. 判断是否被检查组关联
        Long count = permissionDao.findPermissionCountById(id);
        if (count>0){
            //2. 如果被关联，不能删除
            throw new RuntimeException("该权限被角色关联，不能删除！！");
        }else {
            //3. 如果没有关联，直接删除
            permissionDao.delById(id);
        }
    }

    @Override
    public Permission findById(Integer id) {
        return permissionDao.findById(id);
    }

    @Override
    @Transactional
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }

    @Override
    public List<Permission> findAll() {
        return permissionDao.findAll();
    }
}
